<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

class Type extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->model = model("Product.Type");
        $this->ProductModel = model("Product.Product");
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            // echo   $this->SubjectOrderModel->getlastSql();exit;
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    // 添加
    public function add()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $result = $this->model->validate('common/product/Type')->save($params);
            if ($result === FALSE) {
                if ($params['thumb']) {
                    $thumb = substr($params['thumb'], 1);
                    @is_file($thumb) && @unlink($thumb);
                }
                $this->error($this->model->getError());
                exit;
            } else {
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }
    // 编辑
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
            exit;
        }
        $this->assign([
            'row' => $row
        ]);
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $params['id'] = $ids;
            $result = $this->model->validate('common/product/Type')->isUpdate(true)->save($params);
            if ($result === FALSE) {
                if ($params['thumb'] && $params['thumb'] != $row['thumb']) {
                    $thumb = substr($params['thumb'], 1);
                    @is_file($thumb) && @unlink($thumb);
                }
                $this->error($this->model->getError());
                exit;
            } else {
                if ($params['thumb'] != $row['thumb']) {
                    $thumb = substr($row['thumb'], 1);
                    @is_file($thumb) && @unlink($thumb);
                }
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }
    // 删除
    public function del($ids = null)
    {
        $ids = !empty($ids) ? explode(',', $ids) : [];
        $row = $this->model->column('id');
        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error('没有找到该商品分类');
                exit;
            }
        }
        $res = $this->model->select($ids);
        $thumb = array_column($res, 'thumb');
        
        $ids=implode(',',$ids);
        // halt($ids);
        $product=$this->ProductModel->whereIn('typeid',$ids)->select();
        // echo $this->ProductModel->getLastSql();
        // var_dump($ids);
        $pid=implode(',',array_column($product,'id'));
        // var_dump($pid);
        // var_dump(collection($product)->toArray());
        // exit;
        $result = $this->model->destroy($ids);
        if ($result === FALSE) {
            $this->error($this->model->getError());
            exit;
        } else {
            $res=$this->ProductModel->destroy($pid);
            if ($result === FALSE) {
                $this->error($this->ProductModel->getError());
                exit;
            }
            foreach ($thumb as $item) {
                $path = pathinfo($item, PATHINFO_DIRNAME);
                @is_file('.' . $item) && @unlink('.' . $item);
                @rmdir('.' . $path); //删除空目录
            }
            $this->success();
            exit;
        }

    }
}